import numpy as np
import tensorflow as tf

PATCH_COUNT = 4000

gabors = np.load('gabors.npy')
codes = np.load('codes/codesnnTruelamb0.5comps100codes800patches400000epochs16.npy')[:PATCH_COUNT]
basis = np.load('basis/basisnnTruelamb0.5comps100codes800patches400000epochs16.npy')
invPCA = np.load('inversePCA.npy')
v1cMean = np.load('v1cmean.npy')
angles = np.memmap('angles', dtype='float32', mode='r', shape = (4000000, 6, 6, 3, 12, 1))#np.load('angles.npy')
angles = angles[:PATCH_COUNT]

super = np.dot(codes, basis)
super = np.dot(super, invPCA)

super += v1cMean[:super.shape[0]]

super = super.reshape((super.shape[0], 6, 6, 3, 12, 1))

quadPair0 = super * np.cos(angles)
quadPair1 = super * np.sin(angles)

v1 = np.concatenate((quadPair0, quadPair1), axis = -1)

v1Tensor = tf.convert_to_tensor(v1)
v1Tensor = tf.reshape(v1Tensor, [PATCH_COUNT, 6, 6, -1])

gaborsTensor = tf.convert_to_tensor(gabors)
gaborsTensor = tf.reshape(gaborsTensor, [12, 12, 1, -1])

reconst = tf.nn.conv2d_transpose(v1Tensor, gaborsTensor, [PATCH_COUNT, 32, 32, 1], [1, 4, 4, 1], padding = 'VALID')

np.save('reconst.npy', reconst.numpy())
